//
//  ViewController.m
//  SiriShortCutDemo
//
//  Created by zxy on 2019/11/19.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "ViewController.h"
#import "QuickOrderCoffeeIntent.h"

#import <Intents/Intents.h>
#import <IntentsUI/IntentsUI.h>

#import "CoffeeModel.h"
#import "FMDBTool.h"

@interface ViewController ()<INUIAddVoiceShortcutButtonDelegate, INUIAddVoiceShortcutViewControllerDelegate, INUIEditVoiceShortcutViewControllerDelegate>
@property (strong, nonatomic) INUIAddVoiceShortcutButton API_AVAILABLE(ios(12.0)) *shortcutButton;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.shortcutButton.frame = CGRectMake(100, 300, 200, 60);
    [self.view addSubview:self.shortcutButton];
    
    NSUserDefaults *new = [[NSUserDefaults alloc] initWithSuiteName:@"group.ultraSmart.SiriIntension"];
    [new setValue:@"hello" forKey:@"world"];
    [new synchronize];
    
    CoffeeModel *coffee = [[CoffeeModel alloc] initWithID:1 Name:@"摩山咖啡"];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSUserDefaults *new = [[NSUserDefaults alloc] initWithSuiteName:@"group.ultraSmart.SiriIntension"];
    NSLog(@"%@",[new valueForKey:@"world"]);
}

- (INUIAddVoiceShortcutButton *)shortcutButton API_AVAILABLE(ios(12.0)){
    if (!_shortcutButton) {
        _shortcutButton = [[INUIAddVoiceShortcutButton alloc] initWithStyle:INUIAddVoiceShortcutButtonStyleWhiteOutline];
        QuickOrderCoffeeIntent *newIntent = [[QuickOrderCoffeeIntent alloc] init];
        _shortcutButton.shortcut = [[INShortcut alloc] initWithIntent:newIntent];
//        _shortcutButton.translatesAutoresizingMaskIntoConstraints = false;
        _shortcutButton.delegate = self;
    }
    return _shortcutButton;
}

#pragma mark - INUIAddVoiceShortcutButtonDelegate
- (void)presentAddVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)addVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    addVoiceShortcutViewController.delegate = self;
    [self presentViewController:addVoiceShortcutViewController animated:YES completion:nil];
}
 
- (void)presentEditVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)editVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    editVoiceShortcutViewController.delegate = self;
    [self presentViewController:editVoiceShortcutViewController animated:YES completion:nil];
}
 
#pragma mark - INUIAddVoiceShortcutViewControllerDelegate
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error
API_AVAILABLE(ios(12.0)) API_AVAILABLE(ios(12.0)){
    NSLog(@"addVoiceShortcutViewController------%@",error);
    [controller dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - INUIEditVoiceShortcutViewControllerDelegate
- (void)editVoiceShortcutViewControllerDidCancel:(INUIEditVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didUpdateVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didDeleteVoiceShortcutWithIdentifier:(NSUUID *)deletedVoiceShortcutIdentifier API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}

@end
